﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

// add custom .Net DLL's that are part of the project
using p2p.File;
using p2p.File.FileLayout;
using p2p.File.FileManager;
using p2p.Misc.HashGenerator;
using p2p.Misc.Base64;
using System.IO;
using System.Reflection;

namespace p2p
{
    public partial class FileCreatorTestCaseForm : Form
    {
        private p2pfile testFile;

        public FileCreatorTestCaseForm()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string curDir = Directory.GetCurrentDirectory();
            string filePath = String.Format("{0}\\Advert.html", curDir);
            
            webControl1.LoadHTML(new StreamReader(filePath).ReadToEnd() );

            // create the test file
            testFile = new p2pfile();

            // create and instance of the hashGenerator
            Hash hashGen = new Hash();

            // create a fake fileList
            testFile.FileList = new File.FileLayout.parts.FileList();
            testFile.FileList.Directory = new File.FileLayout.parts.Dir[1];
            testFile.FileList.Directory[0] = new File.FileLayout.parts.Dir();
            testFile.FileList.Directory[0].Name = "test folder";
            testFile.FileList.Directory[0].Files = new File.FileLayout.parts.File[2];
            testFile.FileList.Directory[0].Files[0] = new File.FileLayout.parts.File();
            testFile.FileList.Directory[0].Files[0].Name = "test.txt";
            testFile.FileList.Directory[0].Files[0].hash = hashGen.getHash();
            testFile.FileList.Directory[0].Files[0].BlockSize = 1;
            testFile.FileList.Directory[0].Files[0].Blocks = 1;
            testFile.FileList.Directory[0].Files[1] = new File.FileLayout.parts.File();
            testFile.FileList.Directory[0].Files[1].Name = "test2.txt";
            testFile.FileList.Directory[0].Files[1].hash = hashGen.getHash();
            testFile.FileList.Directory[0].Files[1].BlockSize = 1;
            testFile.FileList.Directory[0].Files[1].Blocks = 1;
            testFile.FileList.Files = new File.FileLayout.parts.File[1];
            testFile.FileList.Files[0] = new File.FileLayout.parts.File();
            testFile.FileList.Files[0].Name = "test3.txt";
            testFile.FileList.Files[0].hash = hashGen.getHash();
            testFile.FileList.Files[0].BlockSize = 1;
            testFile.FileList.Files[0].Blocks = 1;

            // create a fake status server
            testFile.StatusServers = new File.FileLayout.parts.StatusServer();
            testFile.StatusServers.Servers = new File.FileLayout.parts.Server[1];
            testFile.StatusServers.Servers[0] = new File.FileLayout.parts.Server();
            testFile.StatusServers.Servers[0].Name = "Local Server";
            testFile.StatusServers.Servers[0].Address = "http://localhost:8088/list.php";

            testFile.Hash = hashGen.getHash();


            treeView1.Nodes.Add(new TreeNode("Loading..."));

            Classes.CreateTreeView ctv = new Classes.CreateTreeView();
            ctv.BuildTreeView(testFile.FileList, treeView1);
        }

        private void saveBrowseBtn_Click(object sender, EventArgs e)
        {
            saveFileDialog1.ShowDialog();
            saveFileDialog1.OverwritePrompt = false;
            savePathTxt.Text = saveFileDialog1.FileName;
        }

        private void loadPathTxt_Enter(object sender, EventArgs e)
        {
            LoadBrowseBtn_Click(sender, e);
        }

        private void savePathTxt_Enter(object sender, EventArgs e)
        {
            saveBrowseBtn_Click(sender, e);
        }

        private void LoadBrowseBtn_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
            loadPathTxt.Text = openFileDialog1.FileName;
        }

        private void SaveBtn_Click(object sender, EventArgs e)
        {
            DialogResult overwrite = DialogResult.OK;
            //testFile
            if(System.IO.File.Exists(savePathTxt.Text)){
                overwrite = MessageBox.Show("File exists do you want to overright", "File Save", MessageBoxButtons.OKCancel);
            }
            if (overwrite == DialogResult.OK)
            {
                FileHandler fh = new FileHandler(savePathTxt.Text);
                fh.WriteFile(testFile);
                MessageBox.Show("File Saved Succefully", "File Save", MessageBoxButtons.OK);
            }
        }

        private void LoadBtn_Click(object sender, EventArgs e)
        {
            //testFile
            if (!System.IO.File.Exists(savePathTxt.Text))
            {
                MessageBox.Show("File does note exist", "File Load", MessageBoxButtons.OK);
            }
            FileHandler fh = new FileHandler(loadPathTxt.Text);
            testFile = (File.FileLayout.p2pfile)fh.ReadFile();
            Classes.CreateTreeView ctv = new Classes.CreateTreeView();
            ctv.BuildTreeView(testFile.FileList, treeView1);
            MessageBox.Show("File Loaded Succefully", "File Load", MessageBoxButtons.OK);
        }
    }
}
